package com.weilus.service.permission;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.weilus.dao.mapper.*;
import com.weilus.dto.request.PRoleDto;
import com.weilus.dto.response.BootstrapTablePage;
import com.weilus.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService {

	@Autowired
	SecurityGroupMapper groupMapper;
	@Autowired
	SecurityGroupAuthorityMapper groupAuthorityMapper;
	@Autowired
	SecurityGroupMemberMapper groupMemberMapper;

	public BootstrapTablePage page(PRoleDto dto) {
		Page<SecurityGroup> page = PageHelper.startPage(dto.getPageNumber(),dto.getPageSize());
		SecurityGroupExample example = new SecurityGroupExample();
		SecurityGroupExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(dto.getGroupName()))criteria.andGroupNameLike(dto.getGroupName());
		if(null != dto.getId())criteria.andIdEqualTo(dto.getId());
		example.setOrderByClause("create_time DESC ");
		groupMapper.selectByExample(example);
		return new BootstrapTablePage(page);
	}

	public List<SecurityGroup> list(){
		SecurityGroupExample example = new SecurityGroupExample();
		example.setOrderByClause("create_time DESC ");
		return groupMapper.selectByExample(example);
	}

	@Transactional
	public Object addRoleRight(Integer roleId, List<String> authorities) {
		//删除角色权限
		SecurityGroupAuthorityExample groupAuthorityExample = new SecurityGroupAuthorityExample();
		groupAuthorityExample.createCriteria().andGroupIdEqualTo(roleId);
		groupAuthorityMapper.deleteByExample(groupAuthorityExample);
		if(!CollectionUtils.isEmpty(authorities)){
			authorities.stream().map(authority->{
				SecurityGroupAuthority groupAuthority = new SecurityGroupAuthority();
				groupAuthority.setAuthority(authority);
				groupAuthority.setGroupId(roleId);
				return groupAuthority;
			}).forEach(groupAuthority->
				groupAuthorityMapper.insertSelective(groupAuthority)
			);
		}
		return true;
	}
	@Transactional
	public Object add(SecurityGroup role) {
		role.setCreateTime(new Date());
		groupMapper.insertSelective(role);
		return true;
	}

	@Transactional
	public boolean update(SecurityGroup role) {
		groupMapper.updateByPrimaryKeySelective(role);
		return true;
	}

	@Transactional
	public boolean delete(Integer id) {
		SecurityGroupAuthorityExample groupAuthorityExample = new SecurityGroupAuthorityExample();
		groupAuthorityExample.createCriteria().andGroupIdEqualTo(id);
		groupAuthorityMapper.deleteByExample(groupAuthorityExample);
		SecurityGroupMemberExample groupMemberExample = new SecurityGroupMemberExample();
		groupMemberExample.createCriteria().andGroupIdEqualTo(id);
		groupMemberMapper.deleteByExample(groupMemberExample);
		groupMapper.deleteByPrimaryKey(id);
		return true;
	}

	public List<String> queryRoleRight(Integer id) {
		SecurityGroupAuthorityExample groupAuthorityExample = new SecurityGroupAuthorityExample();
		groupAuthorityExample.createCriteria().andGroupIdEqualTo(id);
		return groupAuthorityMapper.selectByExample(groupAuthorityExample)
				.stream().map(SecurityGroupAuthority::getAuthority)
				.collect(Collectors.toList());
	}

}
